<?php
/**
 * MetadataController
 *
 * @author fusq
 */
require_once BaseUtil::getModulePath("base")  .  '/controllers/BaseController.php';
require_once BaseUtil::getModulePath("metadata")  .  '/forms/MetadataForm.php';
require_once BaseUtil::getModulePath("metadata")  .  '/services/MetadataService.php';

class Metadata_MetadataController  extends BaseController {

    public function init() {
        $this->view->script = array("metadata.js");
        parent::init();
    }

    public function indexAction(){
        $metadataServcie = new MetadataService();
        $metadatas = $metadataServcie->getMetadatas($this->view, $_POST);
        $this->view->dbGrid = $metadatas;
    }

    public function addAction(){
        $metadataForm = new MetadataForm();
        $this->view->form = $metadataForm;
    }

    public function subaddAction(){
        $metadataForm = new MetadataForm();
        if (!$metadataForm->isValid($_POST)) {
            $this->view->form = $metadataForm;
            return $this->render('add');
        }
        $metadataServcie = new MetadataService();
        $metadataServcie->saveMetadata($metadataForm, $_POST);
        $this->_redirector->gotoUrl('/metadata/metadata/index');
    }

    public function editAction(){
        $id = $this->_getParam("id");
        $metadata = array();
        $metadataOption = array();
        $metadataService = new MetadataService();
        $metadata = $metadataService->getMetadata($id);
        $metadataOption = $metadataService->getOptionsByMetadataId($metadata['id']);
        $metadataForm = new MetadataForm($id);
        $metadataForm->getElement("id")->setValue($metadata['id']);
        $metadataForm->getElement("inputTypeValue")->setValue($metadata['input_type']);
        $metadataForm->getElement("code")->setValue($metadata['code']);
        $metadataForm->getElement("title")->setValue($metadata['title']);
        $metadataForm->getElement("longTitle")->setValue($metadata['long_title']);
        $metadataForm->getElement("inputType")->setValue($metadata['input_type']);
        $metadataForm->getElement("valueRequired")->setValue($metadata['value_required']);
        $metadataForm->getElement("description")->setValue($metadata['description']);
        $metadataForm->getElement("inBasicSearch")->setValue($metadata['in_basic_search']);
        $metadataForm->getElement("inAdvanceSearch")->setValue($metadata['in_advanced_search']);
        $metadataForm->getElement("showInProduct")->setValue($metadata['show_in_product']);
        $metadataForm->getElement("sortOrder")->setValue($metadata['sort_order']);
        $this->view->form = $metadataForm;
        $this->view->metadataOption = $metadataOption;
        $this->view->metadata = $metadata;
        $this->view->id = $id;
    }

    public function subeditAction(){
        $id = $_POST['id'];
        $metadataForm = new MetadataForm($id);
        $metadataService = new MetadataService();
        if (!$metadataForm->isValid($_POST)) {
            $metadata = $metadataService->getMetadata($id);
            $metadataOption = $metadataService->getOptionsByMetadataId($id);
            $this->view->metadataOption = $metadataOption;
            $this->view->metadata = $metadata;
            $this->view->form = $metadataForm;
            $this->view->id = $id;
            return $this->render('edit');
        }
        $metadataService->updateMetadata($metadataForm, $_POST);
        $this->_redirector->gotoUrl('/metadata/metadata/index');
    }

    public function deleteAction(){
        $id = $this->_getParam("id");
        $metadataService = new MetadataService();
        if(!empty($id)){
            $metadataService->deleteMetadata($id);
        }
        $this->_redirector->gotoUrl('/metadata/metadata/index');
    }
}
